//
//  ESCustomNavigationbar.m
//  EasyStyle
//
//  Created by FK on 15/12/18.
//  Copyright © 2015年 FK. All rights reserved.
//

#import "ESCustomNavigationbar.h"
#import "UIButton+ESEnlargeTouchArea.h"

typedef void(^BackActionBlock)();
@interface ESCustomNavigationbar ()
@property (nonatomic, assign) BOOL animating;

@property (nonatomic, copy)BackActionBlock actionBlcok;

@end

@implementation ESCustomNavigationbar


- (instancetype)initWithFrame:(CGRect)frame style:(ESCustomNavigationbarStyle)style customColor:(UIColor *)barColor{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.style = style;
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.navigationBar = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height - 44, self.frame.size.width, 44)];
        self.navigationBar.backgroundColor = [UIColor clearColor];
        self.navigationBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
        [self addSubview:_navigationBar];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, self.navigationBar.frame.size.width - 100, self.navigationBar.frame.size.height)];
        self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.backgroundColor = [UIColor clearColor];
        self.titleLabel.font = [UIFont boldSystemFontOfSize:18];
        

        self.titleLabel.textColor = [UIColor blackColor];
        
        self.titleLabel.shadowOffset = CGSizeZero;
        self.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        [self.navigationBar addSubview:self.titleLabel];
        
        self.backgroundColor = barColor;
        
    }
    
    return self;
}

- (NSString *)title{
    
    if (self.titleLabel.text.length == 0) {
        return nil;
    }
    return self.titleLabel.text;
}

- (void)setTitle:(NSString *)title{
    self.titleLabel.text = title;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer {
    return YES;
}

- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated{
    
    [self setNavigationBarHidden:hidden animated:animated complete:nil];
}

- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated complete:(void(^)())block{
    
    
    if (self.animating) {
        return;
    }
    
    self.animating = YES;
    
    if (!hidden) {
        [self.navigationBar.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            [obj setHidden:NO];
        }];
    }
    
    self.userInteractionEnabled = NO;
    
    
    [UIView animateWithDuration:animated?0.3:0 animations:^{
        CGRect frame = self.frame;
        if (hidden) {
            frame.origin.y = -self.navigationBar.frame.size.height;
        }
        else{
            frame.origin.y = 0;
        }
        self.frame = frame;
    } completion:^(BOOL finished) {
        
        self.userInteractionEnabled = YES;
        self.animating = NO;
        
        if (finished && hidden) {
            [self.navigationBar.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                [obj setHidden:YES];
            }];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            if (block) {
                block();
            }
        });
        
    }];
}




- (void)setLeftBarButtonView:(UIView *)view{
    [self.leftButton removeFromSuperview];
    self.leftButton = view;
//    CGRect frame = view.frame;
//    frame.origin.x = 0 ;
//    frame.origin.y = (self.navigationBar.frame.size.height - view.frame.size.height)/2;
    view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
//    view.frame = frame;
    [self.navigationBar addSubview:view];
}


- (void)setRightBarButtonView:(UIView *)view{
    [self.rightButton removeFromSuperview];
    self.rightButton = view;
    CGRect frame = view.frame;
    frame.origin.x = self.navigationBar.frame.size.width - view.frame.size.width + 2 ;
    frame.origin.y = (self.navigationBar.frame.size.height - view.frame.size.height)/2;
    view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    view.frame = frame;
    [self.navigationBar addSubview:view];
}


- (void)showBackButtonWithActionBlock:(void(^)())actionBlock
{
    if (!self.leftButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        button.frame = CGRectMake(0, 0, 25, 25);
        button.exclusiveTouch = YES;
       // [button setEnlargeEdgeWithTop:5 right:15 bottom:5 left:5];
        button.centerY = self.navigationBar.frame.size.height/2;
        button.left = 10;
        button.backgroundColor = [UIColor clearColor];
        
        if (actionBlock) {
            _actionBlcok = actionBlock;
        }
        [button setImage:[UIImage imageNamed:@"back_ic_01"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"back_ic_02"] forState:UIControlStateHighlighted];
        
        [button addTarget:self action:@selector(navigationGoBack) forControlEvents:UIControlEventTouchUpInside];
        [self setLeftBarButtonView:button];
    }
}

- (void)navigationGoBack
{
    if (_actionBlcok) {
        _actionBlcok();
    }
}

//- (void)addPopUpMenuNewWithIcons:(NSArray *)icons titles:(NSArray *)titles target:(id)target{
//    self.popUpMenuNew = [[SGPopUpMenuNew alloc] initWithIcons:icons titles:titles direction:SGPopUpMenuDirectionUp];
//    self.popUpMenuNew.hidden = YES;
//    self.popUpMenuNew.alpha = 0;
//    
//    if (target!= nil) {
//        [target addTarget:self action:@selector(showPopUpMenuNew) forControlEvents:UIControlEventTouchUpInside];
//    }
//}
//
//- (void)addPopUpMenuWithIcons:(NSArray *)icons titles:(NSArray *)titles target:(id)target{
//    self.popUpMenu = [[SGPopUpMenu alloc] initWithIcons:icons titles:titles direction:SGPopUpMenuDirectionUp];
//    //self.popUpMenu.hidden = YES;
//    self.popUpMenu.alpha = 0;
//    
//    if (target!= nil) {
//        [target addTarget:self action:@selector(showPopUpMenu) forControlEvents:UIControlEventTouchUpInside];
//    }
//}
//
//- (void)showPopUpMenu{
//    if (self.popUpMenu.superview) {
//        return;
//    }
//    
//    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder)
//                                               to:nil
//                                             from:nil
//                                         forEvent:nil];
//    CGRect frame = self.popUpMenu.frame;
//    frame.origin.x = self.navigationBar.frame.size.width - 4 - frame.size.width;
//    frame.origin.y = CGRectGetMaxY(self.navigationBar.frame) + 2;
//    frame = [[UIApplication sharedApplication].keyWindow convertRect:frame fromView:self.navigationBar.superview];
//    self.popUpMenu.frame = frame;
//    [[UIApplication sharedApplication].keyWindow addSubview:self.popUpMenu];
//    [self.popUpMenu show];
//}
//- (void)showPopUpMenuNew{
//    if (self.popUpMenuNew.superview) {
//        return;
//    }
//    
//    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder)
//                                               to:nil
//                                             from:nil
//                                         forEvent:nil];
//    CGRect frame = self.popUpMenuNew.frame;
//    frame.origin.x = self.navigationBar.frame.size.width - 4 - frame.size.width;
//    frame.origin.y = CGRectGetMaxY(self.navigationBar.frame) + 2;
//    frame = [[UIApplication sharedApplication].keyWindow convertRect:frame fromView:self.navigationBar.superview];
//    self.popUpMenuNew.frame = frame;
//    [[UIApplication sharedApplication].keyWindow addSubview:self.popUpMenuNew];
//    [self.popUpMenuNew show];
//}

@end
